<template>
  <view class="ios18-modal-container" :class="{ 'visible': visible, 'center-mode': mode === 'center', 'drawer-mode': mode === 'drawer' }" @click="handleMaskClick">
    <view class="ios18-modal-mask"></view>
    
    <!-- 中心模式 -->
    <view v-if="mode === 'center'" class="ios18-modal-center" :style="{ width: width }" @touchend.prevent.stop>
      <view class="ios18-modal-header" v-if="title">
        <text class="ios18-modal-title">{{ title }}</text>
        <view class="ios18-modal-close" @click="$emit('close')">
          <my-icon name="close" width="40rpx" height="40rpx" color="var(--text-secondary-color)" />
        </view>
      </view>
      <view class="ios18-modal-body">
        <slot></slot>
      </view>
      <view class="ios18-modal-footer" v-if="$slots.footer">
        <slot name="footer"></slot>
      </view>
    </view>
    
    <!-- 抽屉模式 -->
    <view v-if="mode === 'drawer'" class="ios18-modal-drawer" :style="drawerStyle" @touchend.prevent.stop>
      <view class="ios18-drawer-handle">
        <view class="ios18-drawer-handle-bar"></view>
      </view>
      <view class="ios18-drawer-title" v-if="title">{{ title }}</view>
      <view class="ios18-drawer-body">
        <slot></slot>
      </view>
      <view class="ios18-drawer-footer" v-if="$slots.footer">
        <slot name="footer"></slot>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'ios18-modal',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    mode: {
      type: String,
      default: 'center',
      validator: (value) => ['center', 'drawer'].includes(value)
    },
    width: {
      type: String,
      default: '80%'
    },
    height: {
      type: String,
      default: '50vh'
    },
    title: {
      type: String,
      default: ''
    },
    maskClosable: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    drawerStyle() {
      // 如果height是auto，不设置高度，让内容自然撑开
      if (this.height === 'auto') {
        return {};
      }
      return { height: this.height };
    }
  },
  methods: {
    handleMaskClick() {
      if (this.maskClosable) {
        this.$emit('close');
      }
    }
  }
}
</script>

<style>
.ios18-modal-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
  visibility: hidden;
  pointer-events: none;
}

.ios18-modal-container.visible {
  visibility: visible;
  pointer-events: auto;
}

.ios18-modal-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  opacity: 0;
  transition: opacity 0.3s;
}

.visible .ios18-modal-mask {
  opacity: 1;
}

/* 中心模式样式 */
.ios18-modal-center {
  position: relative;
  background-color: var(--card-bg-color, #ffffff);
  border-radius: 24px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
  transform: scale(0.9);
  opacity: 0;
  transition: transform 0.3s, opacity 0.3s;
  overflow: hidden;
  max-width: 90%;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  z-index: 1;
}

.visible.center-mode .ios18-modal-center {
  transform: scale(1);
  opacity: 1;
}

.ios18-modal-header {
  padding: 20px 20px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.ios18-modal-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--text-color, #333333);
}

.ios18-modal-close {
  width: 64rpx;
  height: 64rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 32rpx;
  transition: background-color 0.2s;
}

.ios18-modal-close:active {
  background-color: rgba(var(--text-color-rgb, 51, 51, 51), 0.1);
}

.ios18-modal-body {
  padding: 10px 20px 20px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex: 1;
}

.ios18-modal-footer {
  padding: 16px 20px;
  display: flex;
  justify-content: flex-end;
  border-top: 1px solid var(--border-color, rgba(0, 0, 0, 0.1));
}

/* 抽屉模式样式 */
.ios18-modal-drawer {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: var(--card-bg-color, #ffffff);
  border-top-left-radius: 24px;
  border-top-right-radius: 24px;
  box-shadow: 0 -5px 25px rgba(0, 0, 0, 0.15);
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.23, 1, 0.32, 1);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  /* 确保抽屉内容不会被底部小白条遮挡 */
}

.visible.drawer-mode .ios18-modal-drawer {
  transform: translateY(0);
}

.ios18-drawer-handle {
  width: 100%;
  height: 28px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.ios18-drawer-handle-bar {
  width: 40px;
  height: 5px;
  border-radius: 2.5px;
  background-color: var(--border-color, #e0e0e0);
}

.ios18-drawer-title {
  font-size: 20px;
  font-weight: 600;
  text-align: center;
  padding: 0 20px 16px;
  color: var(--text-color, #333333);
}

.ios18-drawer-body {
  flex: 1;
  padding: 0 20px 20px;
  /* 移除底部安全区域的padding，因为已经在父容器中添加 */
}

.ios18-drawer-footer {
  padding: 16px 20px;
  display: flex;
  justify-content: flex-end;
  border-top: 1px solid var(--border-color, rgba(0, 0, 0, 0.1));
  /* 移除底部安全区域的padding，因为已经在父容器中添加 */
}
</style> 